package net.oschina.app.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint.FontMetricsInt;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import net.oschina.app.R;

/**
 * 
 * @author Darcy www.darcye.com
 *
 */
public class AlphabetBar extends View {
	
	public static final char OTHER_CHARACTOR = '#';
	
	private static final char[] ALPHABETS = new char[27];
	
	private TextPaint mPaint;
	
	private int mUnitHeight;
	private int letterYOffset;
	private boolean mHasInitParams = false;
	
	private OnAlphabetTouchListener mOnAlphabetTouchListener;
	
	private OnSizeChangedListener mOnSizeChangedListener;
	
	static{
		for(char ch = 'A'; ch <= 'Z' ; ++ch){
			ALPHABETS[ch - 'A'] = ch;
		}
		ALPHABETS[26] = OTHER_CHARACTOR;
	}
	
	public AlphabetBar(Context context){
		this(context,null);
	}
	
	public AlphabetBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AlphabetBar);
		final int textSize = a.getDimensionPixelSize(R.styleable.AlphabetBar_alphaTextSize, 12);
		final int textColor = a.getColor(R.styleable.AlphabetBar_alphaTextColor, 0xFF000000);
		mPaint = new TextPaint();
		mPaint.setAntiAlias(true);
		mPaint.setTextSize(textSize);
		mPaint.setColor(textColor);
		a.recycle();
		setClickable(true);
	}

	public void setOnAlphabetTouchListener(OnAlphabetTouchListener l) {
		this.mOnAlphabetTouchListener = l;
	}

	public void setOnSizeChangedListener(
			OnSizeChangedListener l) {
		this.mOnSizeChangedListener = l;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		
		final int width = getWidth();
		if(!mHasInitParams){
			final int paddingTop = getPaddingTop();
			final int height = getHeight() - paddingTop - getPaddingBottom();
			this.mUnitHeight = height / ALPHABETS.length;
			final FontMetricsInt fontMetrics = mPaint.getFontMetricsInt();
			this.letterYOffset = (int) ((mUnitHeight - (fontMetrics.bottom - fontMetrics.top)) * 0.5 - fontMetrics.top + paddingTop);
			mHasInitParams = false;
		}
	
		int letterXOffset;
		String letter;
		for(int i = 0  ; i < ALPHABETS.length ;  ++i){
			letter = String.valueOf(ALPHABETS[i]);
			letterXOffset = (int) ((width - mPaint.measureText(letter)) * 0.5);
			canvas.drawText(letter, letterXOffset, letterYOffset + mUnitHeight * i, mPaint);
		}
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent event) {
		super.dispatchTouchEvent(event);
		return true;
	}
	
	private int originIndex = -1;
	@Override
    public boolean onTouchEvent(MotionEvent event) {
		super.onTouchEvent(event);
		final int action = event.getAction();
		if(action == MotionEvent.ACTION_MOVE){
			final float moveY = event.getY();
			final int newIndex = (int) (moveY / mUnitHeight) ;
			if(originIndex != newIndex && 0 <= newIndex && newIndex < ALPHABETS.length){
				originIndex =  newIndex;
				if(mOnAlphabetTouchListener != null){
					mOnAlphabetTouchListener.onTouchMove(ALPHABETS[newIndex]);
				}
			}
		}else if(action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL){
			originIndex = -1;
			if(mOnAlphabetTouchListener != null)
				mOnAlphabetTouchListener.onTouchCancel();
		}
		return true;
    }
	
	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);
		if(mOnSizeChangedListener != null)
			mOnSizeChangedListener.onSizeChanged(w, h, oldw, oldh);
	}
	
	public interface OnAlphabetTouchListener{
		void onTouchMove(char al);
		
		void onTouchCancel();
	}
	
	public interface OnSizeChangedListener{
		void onSizeChanged(int w, int h, int oldw, int oldh);
	}
}
